package ui.controller;

import dev.tools.DevToolsBootstrap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

@Controller()
@RequestMapping(value = "/dev-tools")
public class SimpleDevToolsController {

    public SimpleDevToolsController() {
        DevToolsBootstrap.attach();
    }

    @RequestMapping(value = "index", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String index() {
        try {
            return DevToolsBootstrap.render("/dev-tools/form");
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @RequestMapping(value = "form", produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String form(String code, String address) {
        try {
            if (address == null || address.isEmpty()
                    || code == null || code.isEmpty()
            ) {
                return "please complete your input";
            }
            return DevToolsBootstrap.distCode(Arrays.asList(address.split("\\,")), code);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @RequestMapping(value = "test", produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String test() {
        return DemoUtils.test();
    }

}